package by.yakusekmaxim.bookgid.webapp.page.home.myLibrary;

import javax.inject.Inject;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book_;
import by.yakusekmaxim.bookgid.datamodel.enums.UserAccount2BookStatus;
import by.yakusekmaxim.bookgid.services.UserAccount2BookService;
import by.yakusekmaxim.bookgid.webapp.app.BasicAuthenticationSession;
import by.yakusekmaxim.bookgid.webapp.page.HomeLayout;

public class MyLibraryPage extends HomeLayout {

	@Inject
	UserAccount2BookService userAccount2BookService;

	UserAccount user = BasicAuthenticationSession.get().getUser();

	private Filter<UserAccount2Book> filter = new Filter<UserAccount2Book>();

	@Override
	protected void onInitialize() {
		super.onInitialize();
		
		
		Link<Object> linkCountAll = (new Link<Object>("linkCountAll") {

			@Override
			public void onClick() {
				MyLibraryPage linkCountAll = new MyLibraryPage();
				setResponsePage(linkCountAll);
			}
		});
		linkCountAll.setEnabled(userAccount2BookService
				.getCountByUserAccount(user) != 0);
		linkCountAll.add(new Label("CountAll", userAccount2BookService
				.getCountByUserAccount(user)));
		add(linkCountAll);

		Link<Object> linkCountIwanttoread = (new Link<Object>(
				"linkCountIwanttoread") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(UserAccount2Book_.status);
				filter.setValue(UserAccount2BookStatus.useraccountbookstatus_iwanttoread);
				System.out.println(filter.getValue().toString());
				MyLibraryPage myLibraryPage = new MyLibraryPage(filter,
						new ResourceModel("p.home.MyLibrary.pageNameLabel.Iwanttoread"));
				setResponsePage(myLibraryPage);
			}
		});
		setFilter(UserAccount2BookStatus.useraccountbookstatus_iwanttoread);
		linkCountIwanttoread.setEnabled(userAccount2BookService
				.getCountByUserAccount(filter, user) != 0);
		linkCountIwanttoread.add(new Label("CountIwanttoread",
				userAccount2BookService.getCountByUserAccount(filter, user)));
		add(linkCountIwanttoread);

		Link<Object> linkCountAlreadyread = (new Link<Object>(
				"linkCountAlreadyread") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(UserAccount2Book_.status);
				filter.setValue(UserAccount2BookStatus.useraccountbookstatus_alreadyread);
				MyLibraryPage myLibraryPage = new MyLibraryPage(filter,
						new ResourceModel("p.home.MyLibrary.pageNameLabel.Alreadyread"));
				setResponsePage(myLibraryPage);
			}
		});
		setFilter(UserAccount2BookStatus.useraccountbookstatus_alreadyread);
		linkCountAlreadyread.setEnabled(userAccount2BookService
				.getCountByUserAccount(filter, user) != 0);
		linkCountAlreadyread.add(new Label("CountAlreadyread",
				userAccount2BookService.getCountByUserAccount(filter, user)));
		add(linkCountAlreadyread);
	}

	public MyLibraryPage() {
		Label label = new Label("pageNameLabel", new ResourceModel(
				"p.home.MyLibrary.pageNameLabel"));
		add(label);
		
		Filter<UserAccount2Book> filter1 = new Filter<UserAccount2Book>();
		filter1.setEnabled(false);
		add(new UserAccount2BookListPanel("userAccount2BookListPanel", filter1));
	}

	public MyLibraryPage(Filter<UserAccount2Book> filter, ResourceModel pageName) {
		add(new Label("pageNameLabel", pageName));
		 add(new UserAccount2BookListPanel("userAccount2BookListPanel", filter));
	}

	@Override
	protected MarkupContainer addPanel() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void setFilter(UserAccount2BookStatus status){
		filter.setEnabled(true);
		filter.setAttribute(UserAccount2Book_.status);
		filter.setValue(status);
		System.out.println(filter.getValue().toString());
	}

}
